{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "# Preamble to mimick book environment\n",
    "import logging\n",
    "from pprint import pprint\n",
    "from sys import stdout as STDOUT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 1\n",
    "class LazyDB(object):\n",
    "    def __init__(self):\n",
    "        self.exists = 5\n",
    "\n",
    "    def __getattr__(self, name):\n",
    "        value = 'Value for %s' % name\n",
    "        setattr(self, name, value)\n",
    "        return value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Before: {'exists': 5}\n",
      "foo:    Value for foo\n",
      "After:  {'exists': 5, 'foo': 'Value for foo'}\n"
     ]
    }
   ],
   "source": [
    "# Example 2\n",
    "data = LazyDB()\n",
    "print('Before:', data.__dict__)\n",
    "print('foo:   ', data.foo)\n",
    "print('After: ', data.__dict__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "exists: 5\n",
      "Called __getattr__(foo)\n",
      "foo:    Value for foo\n",
      "foo:    Value for foo\n"
     ]
    }
   ],
   "source": [
    "# Example 3\n",
    "class LoggingLazyDB(LazyDB):\n",
    "    def __getattr__(self, name):\n",
    "        print('Called __getattr__(%s)' % name)\n",
    "        return super().__getattr__(name)\n",
    "\n",
    "data = LoggingLazyDB()\n",
    "print('exists:', data.exists)\n",
    "print('foo:   ', data.foo)\n",
    "print('foo:   ', data.foo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Called __getattribute__(exists)\n",
      "exists: 5\n",
      "Called __getattribute__(foo)\n",
      "foo:    Value for foo\n",
      "Called __getattribute__(foo)\n",
      "foo:    Value for foo\n"
     ]
    }
   ],
   "source": [
    "# Example 4\n",
    "class ValidatingDB(object):\n",
    "    def __init__(self):\n",
    "        self.exists = 5\n",
    "\n",
    "    def __getattribute__(self, name):\n",
    "        print('Called __getattribute__(%s)' % name)\n",
    "        try:\n",
    "            return super().__getattribute__(name)\n",
    "        except AttributeError:\n",
    "            value = 'Value for %s' % name\n",
    "            setattr(self, name, value)\n",
    "            return value\n",
    "\n",
    "data = ValidatingDB()\n",
    "print('exists:', data.exists)\n",
    "print('foo:   ', data.foo)\n",
    "print('foo:   ', data.foo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 5\n",
    "try:\n",
    "    class MissingPropertyDB(object):\n",
    "        def __getattr__(self, name):\n",
    "            if name == 'bad_name':\n",
    "                raise AttributeError('%s is missing' % name)\n",
    "            value = 'Value for %s' % name\n",
    "            setattr(self, name, value)\n",
    "            return value\n",
    "\n",
    "    data = MissingPropertyDB()\n",
    "    data.foo  # Test this works\n",
    "    data.bad_name\n",
    "except:\n",
    "    logging.exception('Expected')\n",
    "else:\n",
    "    assert False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Before:      {'exists': 5}\n",
      "Called __getattr__(foo)\n",
      "foo exists:  True\n",
      "After:       {'exists': 5, 'foo': 'Value for foo'}\n",
      "foo exists:  True\n"
     ]
    }
   ],
   "source": [
    "# Example 6\n",
    "data = LoggingLazyDB()\n",
    "print('Before:     ', data.__dict__)\n",
    "print('foo exists: ', hasattr(data, 'foo'))\n",
    "print('After:      ', data.__dict__)\n",
    "print('foo exists: ', hasattr(data, 'foo'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Called __getattribute__(foo)\n",
      "foo exists:  True\n",
      "Called __getattribute__(foo)\n",
      "foo exists:  True\n"
     ]
    }
   ],
   "source": [
    "# Example 7\n",
    "data = ValidatingDB()\n",
    "print('foo exists: ', hasattr(data, 'foo'))\n",
    "print('foo exists: ', hasattr(data, 'foo'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 8\n",
    "class SavingDB(object):\n",
    "    def __setattr__(self, name, value):\n",
    "        # Save some data to the DB log\n",
    "        super().__setattr__(name, value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Before:  {}\n",
      "Called __setattr__(foo, 5)\n",
      "After:   {'foo': 5}\n",
      "Called __setattr__(foo, 7)\n",
      "Finally: {'foo': 7}\n"
     ]
    }
   ],
   "source": [
    "# Example 9\n",
    "class LoggingSavingDB(SavingDB):\n",
    "    def __setattr__(self, name, value):\n",
    "        print('Called __setattr__(%s, %r)' % (name, value))\n",
    "        super().__setattr__(name, value)\n",
    "\n",
    "data = LoggingSavingDB()\n",
    "print('Before: ', data.__dict__)\n",
    "data.foo = 5\n",
    "print('After:  ', data.__dict__)\n",
    "data.foo = 7\n",
    "print('Finally:', data.__dict__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 10\n",
    "class BrokenDictionaryDB(object):\n",
    "    def __init__(self, data):\n",
    "        self._data = data\n",
    "\n",
    "    def __getattribute__(self, name):\n",
    "        print('Called __getattribute__(%s)' % name)\n",
    "        return self._data[name]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Called __getattribute__(foo)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n",
      "Called __getattribute__(_data)\n"
     ]
    }
   ],
   "source": [
    "# Example 11\n",
    "try:\n",
    "    data = BrokenDictionaryDB({'foo': 3})\n",
    "    data.foo\n",
    "except:\n",
    "    logging.exception('Expected')\n",
    "else:\n",
    "    assert False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "# Example 12\n",
    "class DictionaryDB(object):\n",
    "    def __init__(self, data):\n",
    "        self._data = data\n",
    "\n",
    "    def __getattribute__(self, name):\n",
    "        data_dict = super().__getattribute__('_data')\n",
    "        return data_dict[name]\n",
    "\n",
    "data = DictionaryDB({'foo': 3})\n",
    "print(data.foo)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
